import { defineStore } from "pinia";
import { ref } from "vue";

const initState = { name: "", avatar: "", id: null };

export const useUserStore = defineStore(
    "user",
    () => {
        const userInfo = ref<IUserInfo>({ ...initState });
        const token = ref<string>("");
        const userType = ref<string>("");
        const setToken = (val: string) => {
            token.value = val;
        };
        const setUserType = (val: string) => {
            userType.value = val;
        };
        const setUserInfo = (val: IUserInfo) => {
            userInfo.value = val;
        };

        const clearUserInfo = () => {
            userInfo.value = { ...initState };
            userType.value = "";
            token.value = "";
        };
        // 一般没有reset需求，不需要的可以删除
        const reset = () => {
            userInfo.value = { ...initState };
        };
        const isLogined = computed(() => !!token.value);

        return {
            userInfo,
            token,
            userType,
            setUserType,
            setToken,
            setUserInfo,
            clearUserInfo,
            isLogined,
            reset,
        };
    },
    {
        persist: true,
    }
);
